﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WorkBasket.Data.Entities;
using WorkBasket.Data.Repository;
using WorkBasket.Infrastructure;
using WorkBasket.Models;

namespace WorkBasket.Controllers.Api
{
    public class GroupPropertyController : ApiController
    {
 
            SOC soc = new SOC(new MyContext());
            [Route("api/groupproperty/GetByParentKey/{GroupID}")]
            [HttpGet]
            public IHttpActionResult GetByParentKey(int GroupID)
            {

                return Json(soc.GroupPropertys.Where(c => c.GroupID == GroupID));
            }

            [HttpPost]
            [Route("api/groupproperty/save")]
            public IHttpActionResult Post([FromBody] GroupProperty value)
            {
                try
                {
                    if (value.GroupPropertyID == 0)
                    {
                        soc.GroupPropertys.Add(value);
                    }
                    else
                    {
                        soc.GroupPropertys.Update(value);
                    }

                    soc.CommitChanges();
                    return Json(new DataReturn(value.PropertyName + " was Saved", value));
                }
                catch (Exception e)
                {
                    return new System.Web.Http.Results.ExceptionResult(e, this);
                }
            }
            [HttpPost]
            [Route("api/GroupProperty/delete")]
            public IHttpActionResult Delete([FromBody]  GroupProperty value)
            {
                try
                {
                    soc.GroupPropertys.Delete(value.GroupPropertyID);
                    soc.CommitChanges();
                    return Json(new DataReturn("Property Deleted", value));
                }
                catch (Exception e)
                {
                    return new System.Web.Http.Results.ExceptionResult(e, this);
                }
            }


         
    }
}
